package com.shuyu.web.controller.api;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.shuyu.common.constant.CacheConstants;
import com.shuyu.common.constant.Constants;
import com.shuyu.common.core.controller.BaseController;
import com.shuyu.common.core.domain.Result;
import com.shuyu.common.core.domain.entity.Customer;
import com.shuyu.common.core.redis.RedisCache;
import com.shuyu.common.exception.ServiceException;
import com.shuyu.common.exception.user.CaptchaException;
import com.shuyu.common.exception.user.CaptchaExpireException;
import com.shuyu.common.utils.SecurityUtils;
import com.shuyu.common.utils.StringUtils;
import com.shuyu.common.utils.VerifyCodeUtil;
import com.shuyu.common.utils.aliyun.SendSmsUtil;
import com.shuyu.domain.CustomerCardTransfer;
import com.shuyu.service.CustomerAssetsService;
import com.shuyu.service.CustomerCardTransferService;
import com.shuyu.service.CustomerService;
import com.shuyu.vo.CardInfoVo;
import com.shuyu.vo.CardParamVo;
import com.shuyu.vo.VerifyCodeVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * 授权登录相关
 * 
 * @author
 */
@RestController
@RequestMapping("/api/nft")
@Api(tags = "前端-NFT相关")
public class NFTController extends BaseController
{
    private static final Logger log = LoggerFactory.getLogger(NFTController.class);
    @Resource
    private CustomerAssetsService customerAssetsService;
    @Resource
    private CustomerService customerService;
    @Resource
    private RedisCache redisCache;
    @Resource
    private CustomerCardTransferService customerCardTransferService;
    @PutMapping("/storage")
    @ApiOperation("NFT入库")
    public Result<CardInfoVo> storage(@RequestBody CardParamVo cardParam){
        if(cardParam == null){
            throw new ServiceException("code不允许为空");
        }
        String code = cardParam.getCode();
        if(StringUtils.isEmpty(code)){
            throw new ServiceException("code不允许为空");
        }
        CardInfoVo cardInfo = customerAssetsService.storage(code);
        return Result.success(cardInfo);
    }

    @GetMapping("/transaction/getSmsCode")
    @ApiOperation("请求下发交易验证码")
    public Result<String> transactionNFT(String mobile,String collectionCode) throws Exception {
        Long userId = SecurityUtils.getLoginUser().getUserId();
        customerAssetsService.startTransactionNFT(collectionCode, userId);
        String msgCode = VerifyCodeUtil.getMsgCode(6);
        String verifyKey = CacheConstants.CAPTCHA_CODE_KEY +"transactionNFT_"+userId;
        redisCache.setCacheObject(verifyKey, msgCode, Constants.CAPTCHA_EXPIRATION*6, TimeUnit.MINUTES);
        Customer customer = customerService.getOne(new LambdaQueryWrapper<Customer>().eq(Customer::getMobile, mobile));
        SendSmsUtil.sendTransfer(mobile, msgCode,customer.getNickname(), collectionCode);
        log.info("请求验证码：{},{},{}",mobile,msgCode,collectionCode);
        return Result.success("操作成功");
    }


    @PostMapping("/transaction/submitCode")
    @ApiOperation("提交交易验证码")
    public Result<String> transactionNFT(@RequestBody VerifyCodeVo verifyCode) throws Exception {
        Long userId = SecurityUtils.getLoginUser().getUserId();
        String verifyKey = CacheConstants.CAPTCHA_CODE_KEY +"transactionNFT_"+userId;
        String verify = redisCache.getCacheObject(verifyKey);
        redisCache.deleteObject(verifyKey);
        if (verify == null)
        {
            throw new CaptchaExpireException();
        }
        if (!verifyCode.equals(verify))
        {
            throw new CaptchaException();
        }
        customerAssetsService.transactionNFT(userId);
        return Result.success("操作成功");
    }

    @GetMapping("/transaction/record/list")
    @ApiOperation("获取转移记录")
    public Result<List<CustomerCardTransfer>> getTransactionRecordList() throws Exception {
        Long userId = SecurityUtils.getLoginUser().getUserId();
        List<CustomerCardTransfer> list = customerCardTransferService.list(new LambdaQueryWrapper<CustomerCardTransfer>().eq(CustomerCardTransfer::getToCustomerId, userId)
                .orderByDesc(CustomerCardTransfer::getTransactionTime));
        return Result.success("操作成功",list);
    }

}
